import * as api from "./api";
import { CreateCrudOptionsProps, CreateCrudOptionsRet, dict } from "@fast-crud/fast-crud";
import { request } from "/src/api/service";

export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
  const pageRequest = async (query) => {
    return await api.GetList();
  };
  const editRequest = async ({ form, row }) => {
    form.id = row.id;
    return await api.UpdateObj(form);
  };
  const delRequest = async ({ row }) => {
    return await api.DelObj(row.id);
  }
  const addRequest = async ({ form }) => {
    return await api.AddObj(form);
  };
  return {
    crudOptions: {
      request: {
        pageRequest,
        addRequest,
        editRequest,
        delRequest
      },
      columns: {
        id: {
          title: "ID",
          key: "id",
          type: "number",
          column: {
            width: 50
          },
          form: {
            show: false
          }
        },
        category: {
          title: "分类",
          key: "category",
          type: "dict-select",
          dict: dict({
            data: [
              { label: "文化教育", value: "文化教育" },
              { label: "体育健康", value: "体育健康" },
              { label: "志愿服务", value: "志愿服务" },
              { label: "节日主题", value: "节日主题" }
            ]

          })
        },
        title: {
          title: "标题",
          key: "title",
          type: "string",
          form: {
            show: true
          }
        },
        ac_description: {
          title:'描述',
          key: "ac_description",
          type: "string",
          form: {
            show: false
          }
        },
        location: {
          title:'位置',
          key: "location",
          type: "string",
          form: {
            show: true
          },

        },
        browseCount:{
          title:'浏览人数',
          key: "browseCount",
          type: "number",
          form: {
            show: true
          }
        },
        "userInfo.username": {
          title:'发布人',
          key: "userInfo.username",
          type: "string",
          form: {
            show: false
          }
        },
        userId: {
          title:'发布人',
          key: "userId",
          column:{
            show:false
          },
          type: "dict-select",
          dict: dict({
            getData({ dict }) {
              return request({
                url: "http://localhost:8000/community/users/",
                method: "get"
              });
            },
            value:'id',
            label:'username'
          })
        }
      }
    }
  };
}
